<template>
  <div>
    <el-dialog
      title="添加权限"
      center
      width="500px"
      :visible.sync="isVisible"
      :close-on-press-escape="false"
      :close-on-click-modal="false"
    >
      <el-form
        size="mini"
        status-icon
        label-width="50px"
        :rules="formRules"
        ref="form"
        :model="form"
      >
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.name" autocomplete="off" placeholder="请输入权限名称" />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input v-model="form.remarks" autocomplete="off" placeholder="请输入权限备注" />
        </el-form-item>
        <el-form-item label="URI" prop="uri">
          <el-input v-model="form.uri" autocomplete="off" placeholder="请输入权限URI" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="mini" @click="isVisible = false">取 消</el-button>
        <el-button
          type="primary"
          size="mini"
          :loading="loading"
          @click="insertHandle('form')"
        >
          确 定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  export default {
    name: 'PermissionInsert',
    props: {
      visible: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        form: {
          name: '',
          remarks: '',
          uri: ''
        },
        formRules: {
          name: [
            { required: true, message: '请输入权限角色', trigger: 'blur' }
          ],
          remarks: [
            { required: true, message: '请输入权限备注', trigger: 'blur' }
          ],
          uri: [
            { required: true, message: '请输入权限URI', trigger: 'blur' }
          ]
        },
        loading: false
      }
    },
    computed: {
      isVisible: {
        get() {
          return this.visible
        },
        set(val) {
          this.$emit('update:visible', val)
        }
      }
    },
    methods: {
      insertHandle(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.loading = true
            this.AjaxUtils.post('/permission/insertPermission', this.form, true).then((response) => {
              this.loading = false
              this.isVisible = false
              this.$emit('reloadTable')
              console.log(response)
            }).catch(() => {
              this.loading = false
            })
          }
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
</style>
